package compare;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import algorithms.LosslessCompressionAlgorithm;

public class Compression 
{
	private long _sizeSrc;
	private long _sizeCmpr;
	private LosslessCompressionAlgorithm _algorithm;
	private File _srcImg;
	private File _cmprImg;

	Compression(File original, File compressed, LosslessCompressionAlgorithm algorithm) {

		_srcImg = original;
		_cmprImg = compressed;
		_algorithm = algorithm;
		try {
			_sizeSrc = Files.size(_srcImg.toPath());
		} catch (IOException e) {
			System.err.format("Caught exception: %s", e.toString());
		}
	}

	public void compress() {

		try {
			_algorithm.encode(_srcImg, _cmprImg);
			_sizeCmpr = Files.size(_cmprImg.toPath());
		} catch (IOException e) {
			System.err.format("Caught exception: %s", e.toString());
		}
	}
	
	public long getSizeBefore() {
		return _sizeSrc;
	}
	
	public long getSizeAfter() {
		return _sizeCmpr;
	}
	
	public double getCompressionRatio() {
		return (double)_sizeSrc/_sizeCmpr;
	}
}